package com.learn.actionplatfo.controller;

import com.learn.actionplatfo.bean.*;
import com.learn.actionplatfo.service.GoodForUserService;
import com.learn.actionplatfo.service.GoodService;
import com.learn.actionplatfo.service.OrderService;
import com.learn.actionplatfo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserSearchController {

    @Autowired
    GoodForUserService goodForUserService;

    @Autowired
    OrderService orderService;

    @Autowired
    GoodService goodService;

    @Autowired
    UserService userService;

    @RequestMapping("/fristClass")
    public String fristClassification(String frist, Model model){
        List<GoodForUser> gfUby1stClass = goodForUserService.getGFUby1stClass(frist);
        model.addAttribute("GFUList",gfUby1stClass);
        return "main";
    }

    @RequestMapping("/bothClass")
    public String bothClass(String frist,String second,Model model){
        List<GoodForUser> gfuList=goodForUserService.getGFUby1stand2ndClass(frist,second);
        model.addAttribute("GFUList",gfuList);
        return "main";
    }

    @RequestMapping("/downPutGFU")
    public String getUnUpGFU(Model model){
        model.addAttribute("GFUList",goodForUserService.getGFUbyState(2));
        return "main";
    }

    @RequestMapping("/getAllGFU")
    public String getAllGFU(Model model){
        model.addAttribute("GFUList",goodForUserService.getAllGFU());
        return "main";
    }

    @RequestMapping("/getGFUByBuyUserNo")
    public String getGFUByBuyUserNo(HttpSession session,Model model){
        User user = (User)session.getAttribute("loginUser");
        List<GoodForUser> gfUbyUpUptUser = goodForUserService.getGFUbyUpUptUser(user.getUserNo());
        model.addAttribute("GFUList",gfUbyUpUptUser);
        return "main";
    }

    /**
     * 查找与用户相关的订单
     * @param session
     * @return
     */
    @RequestMapping("/getGoodOrderPriceForUser")
    public String getGoodOrderPriceForUser(HttpSession session,Model model){
        /**
         *     int orderNo;
         *     String goodName;
         *     String goodUpPutUserName;
         *     String goodBuyUserName;
         *     double orderMoney;
         */

        //根据登录用户id获取订单 获取订单后循环向list里放入数据 需要根据订单获得商品 购买用户 上架用户
        ArrayList<GoodOrderPriceForUser> list=new ArrayList<>();

        User user = (User)session.getAttribute("loginUser");
        int userNo = user.getUserNo();
        List<Order> orderList = orderService.getOrderByUpUptUserOrBuyUser(userNo);
        for(Order order : orderList){
            GoodOrderPriceForUser goodOrderPriceForUser = new GoodOrderPriceForUser();
            goodOrderPriceForUser.setOrderNo(order.getOrderNo());
            int orderGoodNo = order.getOrderGoodNo();
            Good good = goodService.getGoodById(orderGoodNo);
            goodOrderPriceForUser.setGoodName(good.getGoodName());
            User userById = userService.getUserById(order.getOrderUpPutUser());
            goodOrderPriceForUser.setGoodUpPutUserName(userById.getUserName());
            userById=userService.getUserById(order.getOrderBuyUser());
            goodOrderPriceForUser.setGoodBuyUserName(userById.getUserName());
            goodOrderPriceForUser.setOrderMoney(order.getOrderMoney());
            list.add(goodOrderPriceForUser);
        }
        model.addAttribute("GOPUList",list);
        return "order";
    }

    @RequestMapping("/searchGFUByName")
    public String searchGFUByName(String keyword,Model model){
        model.addAttribute("GFUList",goodForUserService.getGFUbyName(keyword));
        return "main";
    }
}
